#include "common.h"
#include "syntax-tree.h"
#include "op-list.h"
#include "mcode.h"

#include "syntax.tab.h"


tree_node * root;
extern FILE * yyin;
int flex_error=0,syntax_error=0;
extern void treenode_scan(tree_node * now,Type ret_type);
extern void undef_func_output();

extern int yydebug;
int main(int argc, char** argv) {
    LOG("HELLO WORLD!");
    FILE * input=stdin,* output=stdout;
    int input_s=1,output_s=1;
    if (argc > 1) {
        if (!(input = fopen(argv[1], "r"))) {
            perror(argv[1]);
            return 1;
        }
        if (argc > 2&&!(output = fopen(argv[2], "w"))) {
            perror(argv[1]);
            return 1;
        }
    }
    if(!input_s){
         //yydebug=1;
        yyin=input;
        if(yyparse()!=0||flex_error||syntax_error) {LOG(flex_error?"FLEX ERR":"BISON ERR");return 0;}
        LOG("PARSE DONE!");
        sym_pre_work();
        extern void semantic_pre_work();
        semantic_pre_work();
        treenode_scan(root,NULL);
        if(undef_func_detect()) undef_func_output();
    }else read_ir(input);
   
    if(!err){
        if(output_s) output_op(output);
        else{
            active_var_analyse(first_op);
            interperter(output,first_op);
        }
    }
    return 0;
}